package com.example.administrator.orchardeveryday.ui;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.example.administrator.orchardeveryday.BaseApp;
import com.example.administrator.orchardeveryday.R;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private Button sinaLogin;
    private Button qqLogin;
    private Button wechatLogin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ShareSDK.initSDK(getApplicationContext());
        initView();
    }

    private void initView() {
        sinaLogin= (Button) this.findViewById(R.id.activity_login_sinaweibo_btn);
        qqLogin= (Button) this.findViewById(R.id.activity_login_qq_btn);
        wechatLogin= (Button) this.findViewById(R.id.activity_login_wechat_btn);
        sinaLogin.setOnClickListener(this);
        qqLogin.setOnClickListener(this);
        wechatLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Platform platform=null;
        switch (v.getId())
        {
            case R.id.activity_login_sinaweibo_btn:
                platform= ShareSDK.getPlatform(SinaWeibo.NAME);
                break;
            case R.id.activity_login_qq_btn:
                platform= ShareSDK.getPlatform(QQ.NAME);
                break;
            case R.id.activity_login_wechat_btn:
                platform= ShareSDK.getPlatform(Wechat.NAME);
                break;
        }
        ThreeLogin(platform);
    }

    private void ThreeLogin(Platform platform) {
        platform.removeAccount();
        platform.removeAccount(true);
        if(platform==null)
        {
            return;
        }
        PlatformDb db=platform.getDb();
        if(db.isValid())
        {

        }
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                String userFace = (String) hashMap.get("avatar_hd");
                String userName= (String) hashMap.get("name");
                long id= (long) hashMap.get("id");
                BaseApp.user.setId(id);
                BaseApp.user.setName(userName);
                Intent intent = new Intent();
                intent.putExtra("userFace", userFace);
                setResult(1, intent);

            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {

            }

            @Override
            public void onCancel(Platform platform, int i) {

            }

        });

        platform.showUser(null);
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        this.finish();
    }
}
